package cn.edu.neusoft.neufooddemo.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.bean.Result;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.UserModel;

public class AlterUserInfoActivity extends BaseActivity implements RetrofitListener<Result>{

    private ImageView alter_return,user_image;
    private EditText et_alter_username,et_alter_password,et_alter_telephone,et_alter_address;
    private Button alter_commit;
    private UserModel userModel;
    private LinearLayout ll_image;
    String srcPath="";

    protected void initViews(){
        setLayout(R.layout.activity_alter_userinfo);
        et_alter_username = (EditText)findViewById(R.id.et_alter_username);
        et_alter_password = (EditText)findViewById(R.id.et_alter_password);
        et_alter_telephone = (EditText)findViewById(R.id.et_alter_telephone);
        et_alter_address = (EditText)findViewById(R.id.et_alter_adress);
        alter_commit = (Button)findViewById(R.id.alter_commit);
        alter_return = (ImageView)findViewById(R.id.alter_return);
        user_image = (ImageView)findViewById(R.id.iv_user_photo);
        ll_image = (LinearLayout) findViewById(R.id.ll_image);
    }

    void initEvents(){
        alter_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commit();
            }
        });
        alter_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ll_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(AlterUserInfoActivity.this,PhotoActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    void initData() {
        Intent intent = getIntent();
        et_alter_username.setText(intent.getStringExtra("username"));
        et_alter_password.setText(getUser_password());
        et_alter_telephone.setText(intent.getStringExtra("telephone"));
        et_alter_address.setText(intent.getStringExtra("adress"));
       SharedPreferences sp=getSPFile();
        srcPath=sp.getString("srcPath","");
        if(!"".equals(srcPath))
        {
            setFileToImage();
        }
    }

    private void commit(){
        //保存图片路径
        SharedPreferences sp=getSPFile();
        SharedPreferences.Editor editor = sp.edit();//获取编辑器
        editor.putString("srcPath", srcPath);
        editor.commit();//提交修改

        userModel = new UserModel();
         userModel.updateUserById(getUser_id(),
                et_alter_username.getText().toString(),
                et_alter_password.getText().toString(),
                et_alter_telephone.getText().toString(),
                 et_alter_address.getText().toString(),
                 this);
    }

    @Override
    public void onSuccess(Result result, int flag) {
        if(result.getSuccess().equals("1")){
            showToast("修改成功");
            finish();
        }else{
            showToast("修改失败");
        }
    }

    @Override
    public void onFail() {
        showToast("网络错误");
    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1)
        {
            Bundle bundle=data.getExtras();
            srcPath=bundle.getString("srcPath");
            if(!srcPath.equals("")) {
                setFileToImage();
            }

        }
    }
  void setFileToImage()
    {
        try {
            FileInputStream f = new FileInputStream(srcPath);
            Bitmap bm = null;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;//图片的长宽都是原来的1/8
            BufferedInputStream bis = new BufferedInputStream(f);
            bm = BitmapFactory.decodeStream(bis, null, options);
            user_image.setImageBitmap(bm);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
